package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.*;
import com.ruoyi.huanghefd.mapper.WorksheetDhExcMapper;
import com.ruoyi.huanghefd.mapper.WorksheetDhLogMapper;
import com.ruoyi.huanghefd.service.WorksheetDhService;
import com.ruoyi.huanghefd.mapper.WorksheetDhMapper;
import com.ruoyi.huanghefd.utils.PageBean;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 */
@Service
public class WorksheetDhServiceImpl extends ServiceImpl<WorksheetDhMapper, WorksheetDh>
    implements WorksheetDhService{

    private static final Logger log = LoggerFactory.getLogger(WorksheetDhServiceImpl.class);

    @Resource
    WorksheetDhMapper worksheetDhMapper;
    @Resource
    WorksheetDhExcMapper worksheetDhExcMapper;
    @Resource
    WorksheetDhLogMapper worksheetDhLogMapper;

    @Override
    public AjaxResult addWorksheetDh(WorksheetDh worksheetDh) {

        int result = 0;
        try {
            //动火票主键
            String strDhId = UUID.randomUUID().toString().replace("-", "");

            //动火票票号
            //pubCommon pubCommon = new pubCommon();
            //String strWsCode = pubCommon.getGenerateOrder(strWsTye,"ws");
            String strDhCode =this.getGenerateOrder(worksheetDh.getDhType());
            log.info("生成动火票号：" + strDhCode);
            worksheetDh.setDhCode(strDhCode);
            worksheetDh.setDhId(strDhId);

            //插入动火票主表
            log.info("新增动火工作票-主表开始>>" + worksheetDh);
            result = worksheetDhMapper.insertAll(worksheetDh);
            log.info("新增动火工作票-主表结束<<" + result);

            //插入动火票明细表-执行人和操作证编号
            if(!CollectionUtils.isEmpty(worksheetDh.getExc())) {

                List<WorksheetDhExc> excs=new ArrayList();
                for (WorksheetDhExc msdtls : worksheetDh.getExc()
                ) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    log.info("uuid="+uuid);
                    WorksheetDhExc exc=new WorksheetDhExc();
                    exc.setDhId(uuid);
                    exc.setDhCode(strDhCode);
                    exc.setDhExecutorid(msdtls.getDhExecutorid());
                    exc.setDhExecutor(msdtls.getDhExecutor());
                    exc.setOperaCard(msdtls.getOperaCard());

                    excs.add(exc);
                }
                log.info("新增动火操作票-执行人和操作证编号明细表开始>>" + excs);
                result = worksheetDhExcMapper.insertBatch(excs);
                log.info("新增动火操作票-执行人和操作证编号明细表结束<<" + result);
            }

            //插入动火票明细表-检测记录
            if(!CollectionUtils.isEmpty(worksheetDh.getLog())) {

                List<WorksheetDhLog> logs=new ArrayList<>();

                for (WorksheetDhLog msdtls : worksheetDh.getLog()
                ) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    log.info("uuid="+uuid);
                    WorksheetDhLog log1=new WorksheetDhLog();
                    log1.setDhId(uuid);
                    log1.setDhCode(strDhCode);
                    log1.setDhExecutorid(msdtls.getDhExecutorid());
                    log1.setTestTime(msdtls.getTestTime());
                    log1.setTestProject(msdtls.getTestProject());
                    log1.setTestValue(msdtls.getTestValue());
                    log1.setTesterid(msdtls.getTesterid());
                    log1.setTester(msdtls.getTester());
                    log1.setRemark(msdtls.getRemark());

                    logs.add(log1);
                }

                log.info("新增动火操作票-检测记录明细表开始>>" + worksheetDh.getLog());
                result = worksheetDhLogMapper.insertBatch(logs);
                log.info("新增动火操作票-检测记录明细表结束<<" + result);
            }

            return AjaxResult.success(strDhCode);
        } catch (Exception e) {
            return AjaxResult.error("动火工作票新增失败", e);
        }
    }

    @Override
    public AjaxResult delWorksheetDh(String strWsDhCode) {

        int result = 0;
        try {
            log.info("删除动火票-主表开始>>");
            result = worksheetDhMapper.delByDhId(strWsDhCode);
            log.info("删除动火票-主表结束<<"+result);

            log.info("删除操作票-子表1开始>>");
            result = worksheetDhExcMapper.delByDhId(strWsDhCode);
            log.info("删除操作票-子表1结束<<"+result);

            log.info("删除动火票-子表2开始>>");
            result = worksheetDhLogMapper.delByDhId(strWsDhCode);
            log.info("删除动火票-子表2结束<<"+result);

            return AjaxResult.success(strWsDhCode);
        } catch (Exception e) {
            return AjaxResult.error("动火票删除失败", e);
        }
    }

    @Override
    public AjaxResult uptWorksheetDh(WorksheetDh worksheetDh) {

        int result = 0;
        try {
            log.info("修改动火票-主表开始>>");
            result = worksheetDhMapper.updatebyDsId(worksheetDh);
            log.info("修改动火票-主表结束<<"+result);

            if(!CollectionUtils.isEmpty(worksheetDh.getExc())){
                log.info("修改动火票-从表1开始>>");
                //result = worksheetDhExcMapper.updateBatchByDhId(worksheetDh.getExc());
                //最好的方式还是先删后插
                worksheetDhExcMapper.delByDhId(worksheetDh.getDhCode());
                worksheetDhExcMapper.insertBatch(worksheetDh.getExc());
                log.info("修改动火票-从表1结束<<"+result);
            }
            if(!CollectionUtils.isEmpty(worksheetDh.getLog())){
                log.info("修改动火票-从表2开始>>");
                //result = worksheetDhLogMapper.updateBatchByDhId(worksheetDh.getLog());
                //最好的方式还是先删后插
                worksheetDhLogMapper.delByDhId(worksheetDh.getDhCode());
                worksheetDhLogMapper.insertBatch(worksheetDh.getLog());
                log.info("修改动火票-从表2结束<<"+result);
            }
            return AjaxResult.success(worksheetDh.getDhCode());
        } catch (Exception e) {
            return AjaxResult.error("动火票修改失败", e);
        }
    }

    @Override
    public AjaxResult queryWorksheetDhs(String strWsDhCode, String strWsDhDept, String strWsDhState, String strUserId, String strMailType) {
        List<WorksheetDh> worksheetDhList = worksheetDhMapper.queryWorksheetDhList(strWsDhCode, strWsDhDept,strWsDhState,strUserId,strMailType);
        if(CollectionUtils.isEmpty(worksheetDhList)){
            return AjaxResult.success("动火票查询为空！");
        }
        return AjaxResult.success(worksheetDhList);
    }

    @Override
    public PageBean queryWorksheetDhByPage(String strDhCode, String strDhDept, String strDhState, String strUserId, String strMailType, String strDhType, int pageNum, int pageSize) {
        PageBean pageBean = new PageBean();
        if (pageNum > 0 && pageSize > 0) {
            int totalRecord =worksheetDhMapper.getWorksheetDhNum(strDhCode, strDhDept,strDhState,strUserId,strMailType,strDhType);
            pageBean = new PageBean(pageNum, pageSize, totalRecord);

            //从该行开始拿数据
            int startIndex = (pageNum - 1) * pageSize;
            pageBean.setStartIndex(startIndex);
            pageBean.setCode(200);

            //数据集合
            List<WorksheetDh> worksheetDhList = worksheetDhMapper.queryWorksheetDhListByPage(strDhCode, strDhDept,strDhState,strUserId,strMailType,strDhType,startIndex, pageSize);
            pageBean.setList(worksheetDhList);
            pageBean.setSuccessMsg("工作票查询成功！");
        } else {
            pageBean.setCode(500);
            pageBean.setFailMsg("工作票查询失败！");
        }
        return pageBean;
    }

    /**
     * @Author gnplum
     * @Description //动火票票号
     * @Date 14:56 2023/11/12
     * @param Type
     * @return java.lang.String
     **/
    private String getGenerateOrder(String Type) {

        log.info("获取票编号=" + Type);
        String strWsCode = "";
        int order = 0;
        try {

            order = worksheetDhMapper.getWsDhCodeNum(Type);
            log.info("order=" + order);
            //DQ1202311110001
            strWsCode = Type + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%03d", order);

            log.info("strWsCode=" + strWsCode);

        } catch (Exception e) {
            strWsCode = "";
            //e.printStackTrace();
            throw  e;
        }
        return strWsCode;
    }
}




